import { defineStore } from 'pinia';
import axios from 'axios';
import { nanoid } from "nanoid";

// export const useTalkStore = defineStore('talk', {
//     actions: {
//         async getATalk() {
//         发请求: 连续解构赋值+重命名
//         let { data: { content: title } } = await axios.get('https://api.uomg.com/api/rand/qinghau?format=json')
//         把接受到的数据包装成一个对象
//         let obj = { id: nanoid(), title }
//         放到数组中
//         this.talkList.unshift(obj)
//       }
//     },

//     state() {
//         return {
//             talkList:JSON.parse(localStorage.getItem('talkList') as string) || null
//         }
//     }
// })
import { reactive } from 'vue';

export const useTalkStore = defineStore('talk', () => {
    // talkList就是state
    const talkList = reactive(
        JSON.parse(localStorage.getItem('talkList') as string) || []
    )

    // getATalk函数相当于action
    async function getATalk() {
        // 发请求: 连续解构赋值+重命名
        let { data: { content: title } } = await axios.get('https://api.uomg.com/api/rand/qinghau?format=json')
        // 把接受到的数据包装成一个对象
        let obj = { id: nanoid(), title }
        // 放到数组中
        talkList.unshift(obj)
    } 
    return {talkList, getATalk}
})